<div class="layuimini-main">
	<div class="layui-form layuimini-form" lay-filter="form-data">
		<div class="layui-form-item">
			<label class="layui-form-label required">企业ID</label>
			<div class="layui-input-inline" style="width: 70%">
				<input type="text" name="corpId" lay-verify="required" lay-reqtext="企业ID不能为空" placeholder="查看路径：企业微信后台-我的企业" autocomplete="off" class="layui-input">
			</div>
			<div class="layui-form-mid layui-word-aux"><a href="https://gitee.com/i512team/dhorse-doc/blob/main/guide/%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1%E7%99%BB%E5%BD%95/%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1%E7%99%BB%E5%BD%95.md" target="_blank">《使用指引》</a></div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">AgentId</label>
			<div class="layui-input-block">
				<input type="text" name="agentId" lay-verify="required" lay-reqtext="AgentId不能为空" placeholder="查看路径：企业微信后台-应用管理" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">Secret</label>
			<div class="layui-input-block">
				<input type="text" name="secret" lay-verify="required" lay-reqtext="Secret不能为空" placeholder="查看路径：企业微信后台-应用管理" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">是否开启</label>
			<div class="layui-input-block">
				<input type="checkbox" name="enable" lay-skin="switch" value="1" lay-text="开启|关闭">
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
			</div>
		</div>
	</div>
</div>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

		$.ajax({
			url: '/globalConfig/query',
			type: 'POST',
			dataType: "json",
			contentType: 'application/json;charset=UTF-8',
			data: JSON.stringify({"itemTypes": [9]}),
			success: function(res){
				form.render();
				if(res.code != "000000"){
					layer.msg(res.message, {icon: 5, shift: 6});
					return false;
				}
				if($.isEmptyObject(res.data)){
					return false;
				}
				//给表单赋值
				var wechat = res.data.wechat;
				form.val("form-data", {
				  "corpId": wechat.corpId
				  ,"agentId": wechat.agentId
				  ,"secret": wechat.secret
				  ,"enable": wechat.enable
				});
				form.render();
			},
			error: function(res){
				layer.msg(res.message, {icon: 5, shift: 6});
			}
		});
		
        //监听提交
        form.on('submit(saveBtn)', function (data) {
			//增加类型
			data.field.itemType = 1;
			$.ajax({
				url: '/globalConfig/wechat/addOrUpdate',
				type: 'POST',
				dataType: "json",
				contentType: 'application/json;charset=UTF-8',
				data: JSON.stringify(data.field),
				success: function(res){
					if(res.code != "000000"){
						layer.msg(res.message, {icon: 5, shift: 6});
						return false;
					}	
					layer.msg("保存成功", {
						icon: 1,
						time: 500,
						shade: 0.01,
						shadeClose: false
					});
				},
				error: function(res){
					layer.msg(res.message, {icon: 5, shift: 6});
				}
			});

            return false;
        });

    });
</script>